package com.ugoodtech.mdcc.client.controller;

import com.ugoodtech.mdcc.client.dto.JsonResponse;
import com.ugoodtech.mdcc.client.service.UserLogService;
import com.ugoodtech.mdcc.client.service.VstarServiceReportService;
import com.ugoodtech.mdcc.core.annotation.LogAt;
import com.ugoodtech.mdcc.core.domain.User;
import com.ugoodtech.mdcc.core.domain.VstarServiceReport.VstarServiceReport;
import com.ugoodtech.mdcc.core.repository.ModuleAuthorityRepository;
import com.ugoodtech.mdcc.core.repository.UserRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

@Api(description = "常规服务报告统计")
@RestController
@RequestMapping("/VstarServiceReport")
public class VstarServiceReportController {

    @Autowired
    private VstarServiceReportService vstarServiceReportService;

    @Autowired
    private com.ugoodtech.mdcc.core.service.VstarCoreServiceReportService serviceReportService;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private ModuleAuthorityRepository moduleAuthorityRepository;

    @Autowired
    private UserLogService userLogService;


    /////////////////////////////改/////////////////////////////////

    private static final Logger logger = LoggerFactory.getLogger(VstarServiceReportController.class);

    @ApiOperation("统计常规服务报告")
    @RequestMapping(value = "serviceReportStat", method = RequestMethod.GET)
    @LogAt("serviceReportStat")
    public JsonResponse serviceReportStat(Principal principal, HttpServletRequest request,
                                          @ApiParam("省份多选") @RequestParam(value = "province", required = false) String province,
                                          @ApiParam("城市") @RequestParam(value = "city", required = false) String city,
                                          @ApiParam("品牌") @RequestParam(value = "brand", required = false) String brand,
                                          @ApiParam("维修站类型") @RequestParam(value = "category", required = false) String category,
                                          @ApiParam("维修站名称") @RequestParam(value = "stationName", required = false) String stationName,
                                          @ApiParam("服务开始时间") @RequestParam(value = "serviceStartTime", required = false) Long serviceStartTime,
                                          @ApiParam("服务结束时间") @RequestParam(value = "serviceEndTime", required = false) Long serviceEndTime,
                                          @ApiParam("开始时间") @RequestParam(value = "startTime", required = false) Long startTime,
                                          @ApiParam("结束时间") @RequestParam(value = "endTime", required = false) Long endTime,
                                          @ApiParam("服务项目") @RequestParam(value = "serviceType", required = false) String serviceType,
                                          @ApiParam("服务次数") @RequestParam(value = "serviceNum", required = false) Integer serviceNum,
                                          @ApiParam("常规服务报告状态") @RequestParam(value = "reportStatus", required = false) String reportStatus,
                                          @ApiParam("技术人员姓名") @RequestParam(value = "userName", required = false) String userName,
                                          @ApiParam("是否填写满意度：true:填写，false:未填写") @RequestParam(value = "type", required = false) Boolean type,
                                          @ApiParam("需跟进报告处理状态") @RequestParam(value = "serviceReportStatus", required = false) String serviceReportStatus,
                                          @ApiParam("是否填写精益钣喷：true:填写，false:未填写") @RequestParam(value = "paintType", required = false) Boolean paintType,
                                          @ApiParam("客户状态ID") @RequestParam(value = "customerStat", required = false) String customerStat,
                                          @ApiParam("客户类型ID") @RequestParam(value = "customerType", required = false) String customerType,
                                          @ApiParam("油漆品牌ID") @RequestParam(value = "paintBrand", required = false) String paintBrand,
                                          @ApiParam("品牌定位ID") @RequestParam(value = "productPosition", required = false) String productPosition,
                                          @ApiParam(value = "维修站编号") @RequestParam(value = "stationNum", required = false) String stationNum,
                                          @ApiParam(value = "部门ID") @RequestParam(value = "department", required = false) String department,
                                          @ApiParam("该维修站是否使用我司增值服务") @RequestParam(value = "payService", required = false) String payService,
                                          @ApiParam("油漆配送时效") @RequestParam(value = "sentTime", required = false) String sentTime,
                                          @ApiParam("经销商上门服务周期") @RequestParam(value = "serviceTime", required = false) String serviceTime) {
        Long time = System.currentTimeMillis();
        logger.info("url {} {}", request.getRequestURL(), request.getContextPath());
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("500", "未找到此用户");
        }
//        ModuleAuthority m = moduleAuthorityRepository.findByName(Constants.REPORT_STATISTICS_WECHAT);
//        boolean b = clientUser.getAuthorities().contains(m);
//        if (b == false) {
//            return JsonResponse.errorResponseWithError("抱歉", "您没有此权限:报告统计（微信端）");
//        }
//        logger.info(clientUser.getId() + "");
        VstarServiceReport vstarServiceReport = serviceReportService.statReport(province,city, brand, category, stationName,
                serviceStartTime, serviceEndTime, startTime, endTime, serviceType, serviceNum,reportStatus,
                userName, type, serviceReportStatus,clientUser.getId(),paintType, customerStat, customerType,
                paintBrand, productPosition, stationNum, department,payService,sentTime,serviceTime);
        if (vstarServiceReport == null) {
            return JsonResponse.errorResponseWithError("抱歉", "筛选错误");
        }
        logger.info("总用时：" + (System.currentTimeMillis() - time));
        return JsonResponse.successResponseWithData(vstarServiceReport);
    }

    @ApiOperation("统计常规服务报告--筛选条件为 维修站 名称 品牌")
    @RequestMapping(value = "getServiceChart", method = RequestMethod.GET)
    public com.ugoodtech.mdcc.core.dto.JsonResponse getServiceChart(Principal principal,
                                                                    @ApiParam("省份多选") @RequestParam(value = "province", required = false) String province,
                                                                    @ApiParam("城市") @RequestParam(value = "city", required = false) String city,
                                                                    @ApiParam("品牌") @RequestParam(value = "brand", required = false) String brand,
                                                                    @ApiParam("维修站类型") @RequestParam(value = "category", required = false) String category,
                                                                    @ApiParam("维修站名称") @RequestParam(value = "stationName", required = false) String stationName,
                                                                    @ApiParam("服务开始时间") @RequestParam(value = "serviceStartTime", required = false) Long serviceStartTime,
                                                                    @ApiParam("服务结束时间") @RequestParam(value = "serviceEndTime", required = false) Long serviceEndTime,
                                                                    @ApiParam("开始时间") @RequestParam(value = "startTime", required = false) Long startTime,
                                                                    @ApiParam("结束时间") @RequestParam(value = "endTime", required = false) Long endTime,
                                                                    @ApiParam("服务项目") @RequestParam(value = "serviceType", required = false) String serviceType,
                                                                    @ApiParam("服务次数") @RequestParam(value = "serviceNum", required = false) Integer serviceNum,
                                                                    @ApiParam("常规服务报告状态") @RequestParam(value = "reportStatus", required = false) String reportStatus,
                                                                    @ApiParam("技术人员姓名") @RequestParam(value = "userName", required = false) String userName,
                                                                    @ApiParam("是否填写满意度：true:填写，false:未填写") @RequestParam(value = "type", required = false) Boolean type,
                                                                    @ApiParam("需跟进报告处理状态") @RequestParam(value = "serviceReportStatus", required = false) String serviceReportStatus,
                                                                    @ApiParam("是否填写精益钣喷：true:填写，false:未填写") @RequestParam(value = "paintType", required = false) Boolean paintType,
                                                                    @ApiParam("客户状态ID") @RequestParam(value = "customerStat", required = false) String customerStat,
                                                                    @ApiParam("客户类型ID") @RequestParam(value = "customerType", required = false) String customerType,
                                                                    @ApiParam("油漆品牌ID") @RequestParam(value = "paintBrand", required = false) String paintBrand,
                                                                    @ApiParam("品牌定位ID") @RequestParam(value = "productPosition", required = false) String productPosition,
                                                                    @ApiParam(value = "维修站编号") @RequestParam(value = "stationNum", required = false) String stationNum,
                                                                    @ApiParam(value = "部门ID") @RequestParam(value = "department", required = false) String department,
                                                                    @ApiParam("该维修站是否使用我司增值服务") @RequestParam(value = "payService", required = false) String payService,
                                                                    @ApiParam("油漆配送时效") @RequestParam(value = "sentTime", required = false) String sentTime,
                                                                    @ApiParam("经销商上门服务周期") @RequestParam(value = "serviceTime", required = false) String serviceTime) {
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return com.ugoodtech.mdcc.core.dto.JsonResponse.errorResponseWithError("500", "未找到此用户");
        }
        return vstarServiceReportService.getServiceChart(clientUser, province,city, brand, category, stationName,
                serviceStartTime, serviceEndTime, startTime, endTime, serviceType, serviceNum,reportStatus,
                userName, type, serviceReportStatus,clientUser.getId(),paintType, customerStat, customerType,
                paintBrand, productPosition, stationNum, department,payService,sentTime,serviceTime);
    }


}
